package com.jiukuaidao.zhulin.module.home;

import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiukuaidao.library.Config;
import com.jiukuaidao.library.base.BaseActivity;
import com.jiukuaidao.library.widget.XViewPager;
import com.jiukuaidao.zhulin.R;
import com.jiukuaidao.zhulin.adapter.MainHomePagerAdapter;
import com.jiukuaidao.zhulin.data.TaskProvider;
import com.jiukuaidao.zhulin.entity.EventShopInfo;
import com.jiukuaidao.zhulin.entity.EventShoppingCartInfo;
import com.jiukuaidao.zhulin.module.commodity.SortFragment;
import com.jiukuaidao.zhulin.module.home.contract.MainContract;
import com.jiukuaidao.zhulin.module.home.presenter.MainPresenter;
import com.jiukuaidao.zhulin.module.shoppingcart.ShoppingCartActivity;
import com.jiukuaidao.zhulin.module.user.LoginActivity;
import com.jiukuaidao.zhulin.module.user.MineFragment;
import com.jiukuaidao.zhulin.util.AddressInfoUtil;
import com.jiukuaidao.zhulin.util.BDUtils;
import com.jiukuaidao.zhulin.util.ShopInfoUtil;
import com.jiukuaidao.zhulin.util.UserInfoUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author : H_Joker
 * Date : 2016/5/25
 * Description :
 */
public class MainActivity extends BaseActivity<MainContract.Presenter> implements MainContract.View, RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.vp_content)
    XViewPager vpContent;
    @BindView(R.id.tab_rb_1)
    RadioButton tabRb1;
    @BindView(R.id.tab_rb_2)
    RadioButton tabRb2;
    @BindView(R.id.tab_rb_3)
    RadioButton tabRb3;
    @BindView(R.id.tab_rg_menu)
    RadioGroup tabRgMenu;
    @BindView(R.id.fab_shopingcart)
    TextView fabShopingcart;
    @BindView(R.id.tv_cartnum)
    TextView tvCartnum;
    @BindView(R.id.rl_shopingcart)
    RelativeLayout rlShopingcart;

    private MainHomePagerAdapter mVpAdapter;

    @Override
    protected View getTargetView() {
        return null;
    }

    @Override
    protected int getEnterDirection() {
        return 0;
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void handleBundle(Bundle bundle) {

    }

    @Override
    protected void init() {
        AddressInfoUtil.cleanAddressInfo();
        BDUtils.start(); //定位
        new MainPresenter(TaskProvider.provideTasksRepository(Config.mContext), this);
        EventBus.getDefault().register(this);
        initViewPager();
        initTab();
    }

    private void initTab() {
        tabRgMenu.setOnCheckedChangeListener(this);
        tabRb1.setChecked(true);
    }

    private void initViewPager() {
        mVpAdapter = new MainHomePagerAdapter(getSupportFragmentManager());
        mVpAdapter.addFragment(HomeFragment.newInstance(HomeFragment.class));
        mVpAdapter.addFragment(SortFragment.newInstance(SortFragment.class));
        mVpAdapter.addFragment(MineFragment.newInstance(MineFragment.class));
        vpContent.setEnableScroll(false);
        vpContent.setOffscreenPageLimit(2);
        vpContent.setAdapter(mVpAdapter);
        changeTab(MainContract.TAB_HOME);
    }

    /**
     * 购物车改变
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetShoppingCart(List<EventShoppingCartInfo> datas) {
        int totalCount = 0;
        for (EventShoppingCartInfo item : datas) {
            totalCount = totalCount + item.getCount();
        }
        changeCart(totalCount);
    }

    /**
     * 修改店铺信息
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetShopInfo(EventShopInfo info) {
        fabShopingcart.setText("￥" + info.getDeliveryLimit() + "元起送");
    }

    @OnClick(R.id.rl_shopingcart)
    public void gotoShoppinCart() {
        String deliveryLimit = String.valueOf(ShopInfoUtil.getDeliveryLimit());
        String fullShipmentFree = ShopInfoUtil.getFullShipmentFree();
        String deliveryPrice = ShopInfoUtil.getDeliveryPrice();
        Bundle bundle = new Bundle();

        bundle.putString("startPrice", deliveryLimit);
        bundle.putString("fullShipmentFree",fullShipmentFree);
        bundle.putString("deliveryPrice",deliveryPrice);
        startLoginJump(UserInfoUtil.isLogin(), ShoppingCartActivity.class, this.getClass(), LoginActivity.class, bundle);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.tab_rb_1:// 首页
                changeTab(MainContract.TAB_HOME);
                break;
            case R.id.tab_rb_2:// 分类
                changeTab(MainContract.TAB_SORT);
                break;
            case R.id.tab_rb_3:// 我的
                changeTab(MainContract.TAB_MINE);
                break;
        }
    }

    @Override
    public void changeTab(int index) {
        switch (index) {
            case MainContract.TAB_HOME:// 首页
                vpContent.setCurrentItem(0, false);
                break;
            case MainContract.TAB_SORT:// 分类
                vpContent.setCurrentItem(1, false);
                break;
            case MainContract.TAB_MINE:// 我的
                vpContent.setCurrentItem(2, false);
                break;
        }
    }

    @Override
    public void changeCart(int count) {
        tvCartnum.setText("" + count);
    }
}
